﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace RayDen.Library.Data.Scene.SCFF
{

    public interface ISCFFSerializable
    {
        IEnumerable<string> ToStringArray(string preffix);
    }

    public class ConfigEntity : IEnumerable<KeyValuePair<string,object>>, ISCFFSerializable
    {
        public string Type;
        public string Name;
        public object this[string name]
        {
            get
            {
                if (!@params.ContainsKey(name))
                {
                    throw new ArgumentException("Parameter " + name + " not found in " + Type + " " + Name);
                }
                return @params[name];

            }
            set
            {
                if (!@params.ContainsKey(name))
                {
                    @params.Add(name, value);
                }
                else
                {
                    @params[name] = value;
                }
            }
        }

        private Dictionary<string, object> @params;

        public ConfigEntity()
        {
            @params = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
        }

        public ConfigEntity(string name, string type, Dictionary<string, object> p)
        {
            this.Name = name;
            this.Type = type;
            this.@params = p;
        }

        public Dictionary<string, object> GetParams()
        {
            return new Dictionary<string, object>(@params);
        }

        public TValue Get<TValue>(string name, TValue @default)
        {
            if (@params.ContainsKey(name))
            {
                return (TValue)@params[name];
            }
            return @default;
        }



        public TValue GetValue<TValue>(string name, TValue @default)
        {
            if (@params.ContainsKey(name))
            {
                return ValueReader.Convert<TValue>(@params[name].ToString());
            }
            return @default;
        }


        public IEnumerable<string> ToStringArray(string preffix)
        {
            return @params.Select(param => string.Format("{0}.{1} {2}", preffix, param.Key, param.Value));
        }

        public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
        {
            return @params.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
    }
}